var express = require('express');
var router = express.Router();
const userService = require('../service/userService');

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.post('/reg',async function(req,res){
  let {username,pwd} = req.body;
  let data = await userService.insert({username,pwd});
  res.send(data);
});

router.post('/login',async function(req,res){
  let {username,pwd} = req.body;
  let data = await userService.findByUsernameAndPwd({username,pwd});
  if(data.length > 0){
    req.session.user = data[0];
    res.send({status:1});
    
  }else{
    res.send({status:0});
  }
});

router.post('/usernameExist',async function(req,res){
  let {username} = req.body;
  let data = await userService.findByUsername(username);
  if(data.length > 0){
    res.send({status:0});
  }else{
    res.send({status:1});
  }
});

module.exports = router;
